package com.wlxk.crawler.core.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlxk.crawler.core.dao.ArticleDAO;
import com.wlxk.crawler.core.dao.ChapterDAO;
import com.wlxk.crawler.core.entity.ArticleDO;
import com.wlxk.crawler.core.entity.ChapterDO;
import com.wlxk.crawler.core.enums.SourceEnum;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description:
 * @author: 21054523
 * @time: 2023/11/20 16:44
 */
public abstract class PipelineManager {


    public void saveArticle(ArticleDAO articleDAO, SourceEnum sourceEnum, Long articleId, String name, String author, String url) {
        if (StringUtils.isEmpty(articleId)) {
            return;
        }
        List<ArticleDO> articleDOS = articleDAO.selectList(
                new LambdaQueryWrapper<ArticleDO>()
                        .eq(ArticleDO::getArticleId, articleId).eq(ArticleDO::getSource, sourceEnum.key));
        if (CollectionUtils.isEmpty(articleDOS)) {
            ArticleDO articleDO = new ArticleDO();
            articleDO.setArticleId(articleId);
            articleDO.setName(name);
            articleDO.setAuthor(author);
            articleDO.setUrl(url);
            articleDO.setSource(sourceEnum.key);
            articleDAO.insert(articleDO);
            return;
        }
        ArticleDO articleDO = articleDOS.get(0);
        articleDO.setName(name);
        articleDO.setAuthor(author);
        articleDO.setUrl(url);
        articleDAO.updateById(articleDO);
    }

    public void saveChapter(ArticleDAO articleDAO, ChapterDAO chapterDAO, SourceEnum sourceEnum, Long articleId, Long chapterId, String name, String url) {
        if (null == articleId || null == chapterId) {
            return;
        }
        List<ArticleDO> articleDOS = articleDAO.selectList(new LambdaQueryWrapper<ArticleDO>().eq(ArticleDO::getArticleId, articleId).eq(ArticleDO::getSource, sourceEnum.key));
        if (CollectionUtils.isEmpty(articleDOS)) {
            return;
        }
        Long atId = articleDOS.get(0).getId();
        List<ChapterDO> chapterDOS = chapterDAO.selectList(
                new LambdaQueryWrapper<ChapterDO>()
                        .eq(ChapterDO::getArticleId, atId)
                        .eq(ChapterDO::getChapterId, chapterId));
        if (CollectionUtils.isEmpty(chapterDOS)) {
            ChapterDO chapterDO = new ChapterDO();
            chapterDO.setArticleId(atId);
            chapterDO.setChapterId(chapterId);
            chapterDO.setName(name);
            chapterDO.setUrl(url);
            chapterDO.setSource(sourceEnum.key);
            chapterDAO.insert(chapterDO);
            return;
        }
        ChapterDO chapterDO = chapterDOS.get(0);
        chapterDO.setName(name);
        chapterDO.setUrl(url);
        chapterDAO.updateById(chapterDO);
    }

    public void saveChapterContent(ArticleDAO articleDAO, ChapterDAO chapterDAO, SourceEnum sourceEnum, Long articleId, Long chapterId, String name, String url, String content, Boolean fg) {
        if (StringUtils.isEmpty(articleId) || StringUtils.isEmpty(chapterId)) {
            return;
        }
        List<ArticleDO> articleDOS = articleDAO.selectList(new LambdaQueryWrapper<ArticleDO>().eq(ArticleDO::getArticleId, articleId).eq(ArticleDO::getSource, sourceEnum.key));
        if (CollectionUtils.isEmpty(articleDOS)) {
            return;
        }
        Long atId = articleDOS.get(0).getId();
        List<ChapterDO> chapterDOS = chapterDAO.selectList(
                new LambdaQueryWrapper<ChapterDO>()
                        .eq(ChapterDO::getArticleId, atId)
                        .eq(ChapterDO::getChapterId, chapterId));
        if (CollectionUtils.isEmpty(chapterDOS)) {
            ChapterDO chapterDO = new ChapterDO();
            chapterDO.setArticleId(atId);
            chapterDO.setChapterId(chapterId);
            chapterDO.setName(name);
            chapterDO.setUrl(url);
            chapterDO.setContent(content);
            chapterDO.setStatus(1);
            chapterDO.setSource(sourceEnum.key);
            chapterDAO.insert(chapterDO);
            return;
        }
        ChapterDO chapterDO = chapterDOS.get(0);
        if (fg || null == chapterDO.getContent()) {
            chapterDO.setContent(content);
        } else {
            chapterDO.setContent(chapterDO.getContent() + content);
        }
        chapterDO.setStatus(1);
        chapterDAO.updateById(chapterDO);
    }

}
